package com.example.cardemo.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.cardemo.BrandlistActivity
import com.example.cardemo.R
import com.example.cardemo.bean.BrandBean
import com.example.cardemo.databinding.ItemBrandBinding

class BrandAdapter(var ctx: Context, var mDatas: MutableList<BrandBean.ResultBean>) : RecyclerView.Adapter<BrandAdapter.BrandHolder>() {
    private val inflater = LayoutInflater.from(ctx)

    class BrandHolder(val binding: ItemBrandBinding) : RecyclerView.ViewHolder(binding.root)

    fun addDatas(list: List<BrandBean.ResultBean>) {
        mDatas.addAll(list)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BrandHolder {
        val binding: ItemBrandBinding = DataBindingUtil.inflate(inflater, R.layout.item_brand, parent, false)
        return BrandHolder(binding)
    }

    override fun getItemCount(): Int {
        return mDatas.size
    }

    override fun onBindViewHolder(holder: BrandHolder, position: Int) {
        val bean = mDatas[position]
        holder.binding.item = bean
        holder.binding.root.setOnClickListener {
            val intent = Intent(ctx, BrandlistActivity::class.java)
            intent.putExtra("bean", bean)
            Log.d("BrandAdapter", "Intent created with bean: $bean")
            ctx.startActivity(intent)
        }
    }
}